package com.sgb.member.view_model;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.entity.MemberLevelApproveBean;
import com.sgb.member.entity.MemberLeverBean;
import com.sgb.member.entity.MemberUpdateLevelBean;
import com.sgb.member.service.MemberAPI;
import com.sgb.member.weight.SimpleResult;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.view.selection.CommonPopEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;

public class LeverSettingEditModel extends AndroidViewModel {

    public MutableLiveData<Boolean> mSubmit = new MutableLiveData<>();
    public MutableLiveData<MemberLeverBean> mLeverDetail = new MutableLiveData<>();
    public MutableLiveData<Boolean> dialog = new MutableLiveData<>();
    private final MemberAPI api;

    public LeverSettingEditModel(@NonNull Application application) {
        super(application);
        api = MemberConfig.getInstance().getApi();
    }

    private OptionsPickerView<CommonPopEntity> mOption5;
    private List<CommonPopEntity> mPop5;

    public void showPersonnelRequirementsPop(ItemInputTextView textView, SimpleResult<CommonPopEntity> simpleResult) {
        if (mOption5 == null) {
            mPop5 = new ArrayList<>();
            // 1~50人、50~100人、100~500人、500~1000人、1000~2000人、2000人以上
            mPop5.add(new CommonPopEntity("1", "1~50人"));
            mPop5.add(new CommonPopEntity("2", "50~100人"));
            mPop5.add(new CommonPopEntity("2", "100~500人"));
            mPop5.add(new CommonPopEntity("2", "500~1000人"));
            mPop5.add(new CommonPopEntity("2", "1000~2000人"));
            mPop5.add(new CommonPopEntity("2", "2000人以上"));
            mOption5 = new OptionsPickerBuilder(textView.getContext(), new OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    String name = mPop5.get(options1).getTypeName();
                    textView.getContent_edt().setText(name);
                    if (simpleResult != null) {
                        simpleResult.onResult(mPop5.get(options1));
                    }
                }
            }).setCancelColor(textView.getContext().getResources().getColor(R.color.color_title_small)).setLineSpacingMultiplier(2.2f)
                    .setCancelColor(0xFF666666).setTitleColor(0xFF969696).setSubCalSize(15).setTitleSize(15)
                    .setContentTextSize(15)
                    .build();
            mOption5.setTitleText("人员要求");
        }
        mOption5.setPicker(mPop5);
        mOption5.setSelectOptions(0);
        mOption5.show();
    }

    public void submitLever(MemberLeverBean bean) {
        dialog.setValue(true);
        api.updateRatLevel(bean).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                mSubmit.setValue(true);
                dialog.setValue(false);
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                super.onFailure(call, t);
                dialog.setValue(false);
            }
        });
    }

    public void getLeverDetail(MemberLeverBean bean) {
        dialog.setValue(true);
        api.getMixMemberLevelApproveDetail(bean.getId()).enqueue(new ICallBack<BaseEntity<MemberLeverBean>>() {
            @Override
            public void onSuccess(BaseEntity<MemberLeverBean> data) {
                dialog.setValue(false);
                mLeverDetail.setValue(data.getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<MemberLeverBean>> call, Throwable t) {
                super.onFailure(call, t);
                dialog.setValue(false);
            }
        });
    }
}
